package Controller

import (
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"mbti/Global"
	"mbti/model"
	"time"
)

func InitQuestions(context *gin.Context) {
	fmt.Println("初始化题库...")
	//var requestQuestions = model.Questions{}
	//json.NewDecoder(context.Request.Body).Decode(&requestQuestions)
	//fmt.Println("")
	var questions []model.Questions
	//err := Global.Db.Where("module = ?", "1").Find(&questions).Error
	err := Global.Db.Order("num").Find(&questions).Error
	if err != nil {
		fmt.Println("初始化题库失败")
		context.JSON(200, gin.H{
			"code": 400,
			"msg":  "初始化题库失败",
		})
		return
	}
	//fmt.Println("即将返回的题库：", questions)
	context.JSON(200, gin.H{
		"code": 200,
		"data": questions,
		"msg":  "初始化题库成功",
	})
}

func mapToJson(result interface{}) string {
	// map转 json str
	jsonBytes, _ := json.Marshal(result)
	jsonStr := string(jsonBytes)
	return jsonStr
}

type param struct {
	Param1 []string `json:"param1"`
	Param2 string   `json:"param2"`
}

func CommitAnswer(context *gin.Context) {
	fmt.Println("进入commitAnswer")
	var request = param{}
	json.NewDecoder(context.Request.Body).Decode(&request)
	answerlist := request.Param1
	result := request.Param2
	fmt.Println("测一下param1", answerlist)
	fmt.Println("测一下param2", result)

	//requestAnswer := make(map[string]interface{})

	//err := context.ShouldBind(&requestAnswer)
	//if err != nil {
	//	return
	//}
	//fmt.Println("requestAnswer = ", requestAnswer)
	//answerList := requestAnswer["param1"].(interface{})
	//fmt.Println("测一下这个", answerList)
	//user := requestAnswer["user"].(interface{})
	//id := requestAnswer["id"].(interface{})
	//log.Println("shoudaod xuesheng", user)
	//name := requestAnswer["name"].(interface{})

	//var HumanalityList [8]int
	var str string

	//for k, v := range answerList.([]interface{}) {
	for _, v := range answerlist {
		str += fmt.Sprintf("%v", v)
	}
	//fmt.Println("测一下答案", answerList)
	fmt.Println("测一下答案2", str)

	currentTime := time.Now().Format("2006-01-02 15:04:05") //获取当前时间，类型是Go的时间类型Time
	fmt.Println(currentTime)

	score := model.Test_results{
		SID: Global.Sno,
		//MAJOR_NAME:  major,
		TEST_RESULT: result,
		TEST_TIME:   currentTime,
		Answerlist:  str,
	}
	Global.Db.Create(&score)

}

//管理员查看考试信息

// EditResult 修改考试记录
func EditResult(context *gin.Context) {
	var requestResult = model.Test_results{}
	json.NewDecoder(context.Request.Body).Decode(&requestResult)
	fmt.Println("即将更改以下日期的数据 = ", requestResult.TEST_TIME)

	err := Global.Db.Where("test_time = ?", requestResult.TEST_TIME).Updates(requestResult).Error
	if err != nil {
		context.JSON(200, gin.H{
			"code": 400,
			"msg":  "修改失败",
		})
		return
	}
	context.JSON(200, gin.H{
		"code": 200,
		"msg":  "更新成功",
	})

}

// DelResult 删除考试记录
func DelResult(context *gin.Context) {
	var requestResult = model.Test_results{}
	json.NewDecoder(context.Request.Body).Decode(&requestResult)
	fmt.Println("要删除的Result = ", requestResult.TEST_TIME)
	err := Global.Db.Where("test_time = ?", requestResult.TEST_TIME).Delete(requestResult).Error
	if err != nil {
		context.JSON(200, gin.H{
			"code": 400,
			"msg":  "删除失败",
		})
		return
	}
	context.JSON(200, gin.H{
		"code": 200,
		"msg":  "删除成功",
	})
}

// ShowResult 显示考试记录
func ShowResult(context *gin.Context) {
	var result []model.Test_results
	Global.Db.Find(&result)
	//fmt.Println("即将返回的考试结果：", result)
	context.JSON(200, gin.H{
		"code": 200,
		"msg":  "生成考试记录成功",
		"data": result,
	})
}

type count struct {
	Ptype string `json:"ptype"`
	Num   int    `json:"num"`
}

func CountNum(context *gin.Context) {
	var INFPnum int
	var INTJnum int
	var INTPnum int
	var ENTPnum int
	var ENTJnum int
	var ENFJnum int
	var INFJnum int
	var ENFPnum int
	var ESFPnum int
	var ISFPnum int
	var ISTPnum int
	var ESTPnum int
	var ISFJnum int
	var ESFJnum int
	var ESTJnum int
	var ISTJnum int
	var countlist = make([]count, 0)
	Global.Db.Raw("select count_INFP()").Find(&INFPnum)
	Global.Db.Raw("select count_INTJ()").Find(&INTJnum)
	Global.Db.Raw("select count_INTP()").Find(&INTPnum)
	Global.Db.Raw("select count_ENTP()").Find(&ENTPnum)
	Global.Db.Raw("select count_ENTJ()").Find(&ENTJnum)
	Global.Db.Raw("select count_ENFJ()").Find(&ENFJnum)
	Global.Db.Raw("select count_INFJ()").Find(&INFJnum)
	Global.Db.Raw("select count_ENFP()").Find(&ENFPnum)
	Global.Db.Raw("select count_ESFP()").Find(&ESFPnum)
	Global.Db.Raw("select count_ISFP()").Find(&ISFPnum)
	Global.Db.Raw("select count_ISTP()").Find(&ISTPnum)
	Global.Db.Raw("select count_ESTP()").Find(&ESTPnum)
	Global.Db.Raw("select count_ISFJ()").Find(&ISFJnum)
	Global.Db.Raw("select count_ESFJ()").Find(&ESFJnum)
	Global.Db.Raw("select count_ESTJ()").Find(&ESTJnum)
	Global.Db.Raw("select count_ISTJ()").Find(&ISTJnum)
	countlist = append(countlist, count{"INFP", INFPnum})
	countlist = append(countlist, count{"INTJ", INTJnum})
	countlist = append(countlist, count{"INTP", INTPnum})
	countlist = append(countlist, count{"ENTP", ENTPnum})
	countlist = append(countlist, count{"ENTJ", ENTJnum})
	countlist = append(countlist, count{"ENFJ", ENFJnum})
	countlist = append(countlist, count{"INFJ", INFJnum})
	countlist = append(countlist, count{"ENFP", ENFPnum})
	countlist = append(countlist, count{"ESFP", ESFPnum})
	countlist = append(countlist, count{"ISFP", ISFPnum})
	countlist = append(countlist, count{"ISTP", ISTPnum})
	countlist = append(countlist, count{"ESTP", ESTPnum})
	countlist = append(countlist, count{"ISFJ", ISFJnum})
	countlist = append(countlist, count{"ESFJ", ESFJnum})
	countlist = append(countlist, count{"ESTJ", ESTJnum})
	countlist = append(countlist, count{"ISTJ", ISTJnum})
	//Global.Db.Raw("select count_INTJ()").First(&countlist)
	fmt.Println("各类的人数", countlist)
	context.JSON(200, gin.H{
		"code": 200,
		"msg":  "返回人格数量成功",
		"data": countlist,
	})
}
